<template>
	<!-- 钱包 -->
	<view class="container">
		<view class="top">
			<view class="title">
				余额
			</view>
			<view class="num">
				￥{{info.balance}}
			</view>
			<view class="btnS">
				<view class="item" style="border-right:4upx solid #fe7246;" @tap="goRecharge">
					充值
				</view>
				<view class="item" @tap="goCash">
					提现
				</view>
			</view>
		</view>
		<view class="mid">
			<view class="title">
				获取优惠卷
			</view>
			<view class="card">
				<view class="jindu">
					<cmd-progress :percent="percent" :show-info="false" stroke-color="#FF825B" :stroke-width="12" status="active"></cmd-progress>
				</view>
				<view class="text">
					距离赠送优惠卷还差￥{{info.disparity}}
				</view>
				<view class="btn" @tap="getCoupon">
					立即获取
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="title">
				明细
			</view>
			<view class="Record" v-for="item in info.detailed" :key="item.id">
				<view class="left">
					<view class="project">
						{{item.title}}
					</view>
					<view class="time">
						{{item.created_at}}
					</view>
				</view>
				<view class="right" v-if="item.type==0">
					+￥{{item.money}}
				</view>
				<view class="right" v-if="item.type==1">
					-￥{{item.money}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import cmdProgress from "@/components/cmd-progress/cmd-progress.vue"
	export default {
		components:{
			cmdProgress
		},
		data() {
			return {
				a:false,
				token:'',
				page:1,
				info:'',
				percent:0//进度条
			};
		},
		onShow() {
			// console.log("显示")
			// if(isloding){
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				uni.request({
					url: this.webUrl+'/user/my/wallet',
					method: 'GET',
					header:{
						token:this.token
					},
					data: {
						page:1
					},
					success: res => {
						// console.log(res)
						uni.hideLoading()
						if(res.data.code==0){
							this.info=res.data.data
							if(res.data.data.disparity==0){
								this.percent=100
							}else{
								this.percent=res.data.data.disparity/res.data.data.require
								// console.log(this.percent)
							}
						}else if(res.data.code==3){
							uni.showToast({
								title: res.data.msg,
								icon:'none',
								duration:2000
							});
						}else{
							uni.showToast({
								title: '网络错误，请重试',
								icon:'none',
								duration:2000
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误，请重试',
							icon:'none',
							duration:2000
						});
					},
					complete: () => {}
				});
			// }
		},
		onLoad(e) {
			this.token=e.token
			uni.showLoading({
				title: '加载中',
				mask: false
			});
			uni.request({
				url: this.webUrl+'/user/my/wallet',
				method: 'GET',
				header:{
					token:e.token
				},
				data: {
					page:1
				},
				success: res => {
					// console.log(res)
					uni.hideLoading()
					if(res.data.code==0){
						this.info=res.data.data
						if(res.data.data.disparity==0){
							this.percent=100
						}else{
							this.percent=res.data.data.disparity/res.data.data.require
							// console.log(this.percent)
						}
					}else if(res.data.code==3){
						uni.showToast({
							title: res.data.msg,
							icon:'none',
							duration:2000
						});
					}else{
						uni.showToast({
							title: '网络错误，请重试',
							icon:'none',
							duration:2000
						});
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络错误，请重试',
						icon:'none',
						duration:2000
					});
				},
				complete: () => {}
			});
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中',
				mask: false
			});
			this.page++
			uni.request({
				url: this.webUrl+'/user/my/wallet',
				method: 'GET',
				header:{
					token:this.token
				},
				data: {
					page:this.page
				},
				success: res => {
					uni.hideLoading()
					if(res.data.code==0){
						if(res.data.data.detailed.length==0){
							uni.showToast({
								title: '没有更多了',
								icon:'none',
								duration:2000
							});
						}else{
							let arr=res.data.data.detailed.map(item=>{
								this.info.detailed.push(item)
							})
						}
					}else if(res.data.code==3){
						uni.showToast({
							title: res.data.msg,
							icon:'none',
							duration:2000
						});
					}else{
						uni.showToast({
							title: '网络错误，请重试',
							icon:'none',
							duration:2000
						});
					}
				},
				fail: () => {},
				complete: () => {}
			});
		},
		methods:{
			goRecharge(){
				uni.navigateTo({
					url: 'Recharge/Recharge?token='+this.token+'&balance='+this.info.balance,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			getCoupon(){//获取优惠券
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				uni.request({
					url: this.webUrl+'/user/my/getCoupon',
					method: 'POST',
					header:{
						token:this.token
					},
					data: {},
					success: res => {
						console.log(res)
						uni.hideLoading()
						if(res.data.code==3){
							uni.showToast({
								title: res.data.msg,
								icon:'none',
								duration:2000
							});
						}else if(res.data.code==0){
							uni.showToast({
								title: '领取成功',
								icon:'none',
								duration:2000
							});
							setTimeout(()=>{
								uni.navigateTo({
									url: '../index/index',
									success: res => {},
									fail: () => {},
									complete: () => {}
								});
							},2000)
						}else{
							uni.showToast({
								title: '网络错误',
								icon:'none',
								duration:2000
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			goCash(){
				uni.navigateTo({
					url: 'cash/cash?balance='+this.info.balance,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	uni-page-body{
		height: 100%;
	}
	page{
		height: 100%;
	}
	.container{
		position: absolute;
		left: 0;
		bottom: 0;
		top: 0;
		right: 0;
		background: #f2f2f2;
		.top{
			border-top: 2upx solid #EFEFEF;
			display: flex;
			background: #FFFFFF;
			border-radius: 30upx;
			padding-bottom: 52upx;
			flex-direction: column;
			align-items: center;
			.title{
				font-size:36upx;
				line-height: 36upx;
				font-family:Microsoft YaHei;
				font-weight:bold;
				color:rgba(52,52,52,1);
				margin-top: 30upx;
			}
			.num{
				font-size:48upx;
				line-height: 48upx;
				font-family:Microsoft YaHei;
				font-weight:bold;
				color:rgba(52,52,52,1);
				margin-top: 40upx;
			}
			.btnS{
				margin-top: 40upx;
				display: flex;
				width: 500upx;
				font-size: 30upx;
				line-height: 30upx;
				font-family:Microsoft YaHei;
				font-weight:bold;
				border-radius: 40upx;
				height: 30upx;
				padding: 30upx 0;
				line-height: 30upx;
				color: #FFFFFF;
				background-color: #fe5621;
				.item{
					width: 50%;
					text-align: center;
				}
			}
		}
		.mid{
			padding-bottom: 30upx;
			background: #FFFFFF;
			width: 100%;
			.title{
				border-top-right-radius: 36upx;
				border-bottom-right-radius: 36upx;
				font-size: 32upx;
				padding-left: 30upx;
				width: 90%;
				height: 60upx;
				background-color: #fe5621;
				color: #FFFFFF;
				line-height: 60upx;
			}
			.card{
				width: 690upx;
				height: 300upx;
				overflow: hidden;
				margin: 30upx auto;
				background: #FFE7E0;
				border-radius: 20upx;
				.jindu{
					margin: 40upx auto 19upx;
					width: 600upx;
					// margin-top: 30upx;
				}
				.text{
					text-align: right;
					font-size: 24upx;
					margin-right: 40upx;
					color:rgba(49,49,49,1);
				}
				.btn{
					width: 560upx;
					text-align: center;
					height: 60upx;
					margin: 80upx auto 0;
					color: #FFFFFF;
					font-size: 30upx;
					line-height: 60upx;
					border-radius: 30upx;
					background:rgba(253,120,78,1);
				}
			}
		}
		.bottom{
			// margin-top: 30upx;
			background: #FFFFFF;
			border-radius: 30upx;
			width: 100%;
			.title{
				border-top-right-radius: 30upx;
				border-bottom-right-radius: 30upx;
				font-size:32upx;
				font-family:Microsoft YaHei;
				font-weight:bold;
				padding-left: 30upx;
				width: 92%;
				height: 60upx;
				background-color: #fe5621;
				color: #FFFFFF;
				line-height: 60upx;
			}
			.Record{
				width: 90%;
				margin: 0 auto;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1upx solid #f2f2f2;
				.left{
					.project{
						font-size:26upx;
						margin: 12upx 0 17upx 12upx;
						font-family:Microsoft YaHei;
						font-weight:400;
						color:rgba(52,52,52,1);
					}
					.time{
						font-size:24upx;
						margin-left: 15upx;
						margin-bottom: 20upx;
						font-family:Microsoft YaHei;
						font-weight:400;
						color:rgba(52,52,52,1);
					}
				}
				.right{
					font-size:26upx;
					font-family:Microsoft YaHei;
					font-weight:400;
					color:rgba(52,52,52,1);
				}
			}
		}
	}
</style>
